﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.SearchReport
{
    public partial class SamplingReport : PageBase
    {
        PPARBO pbo = new PPARBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                btnSubmit_Click(null, null);
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string sql = " where 1=1 ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (txtSupplierNo.Text.Trim() != "")
                sql += " and supplierpartno='" + txtSupplierNo.Text.Trim() + "' ";
            if (txtCreateStart.Text.Trim() != "")
                sql += " and trunc(endtime)>=to_date( '" + txtCreateStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtCreateEnd.Text.Trim() != "")
                sql += " and trunc(endtime)<=to_date( '" + txtCreateEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtSamplingReportNo.Text.Trim()!="")
                sql += " and samplingreportno like '%" + txtSamplingReportNo.Text.Trim() + "%' ";
            if (txtPartNo.Text.Trim()!="")
                sql += " and bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (ddlStatus.SelectedValue != "All")
            {
                if (ddlStatus.SelectedValue == "5")
                    sql += " and (status=5 or (status=6 and FINALSTATUS=0)) ";
                else
                sql += " and status=" + ddlStatus.SelectedValue + " ";
            }
            if (ddlReason.SelectedValue != "All")
                sql += " and  reasonforsampling='" + ddlReason.SelectedValue + "' ";
            if (txtVersion.Text.Trim()!="")
                sql += " and PPARVERSION like '%" + txtVersion.Text.Trim() + "%' ";
            sql += " and status=7 ";
            DataSet ds = pbo.GetReportForSearch(sql);
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            dgList.DataBind();
        }

        public string ShowAuditStatus(string Status, string FINALSTATUS)
        {
            string result = "";
            result = "<span style='color:red'>已提交</span>";

            switch (Status.ToLower())
            {
                case "0":
                    result = "<span style='color:blue'></span>";
                    break;
                case "1":
                    result = "<span style='color:red'>PPAR created</span>";
                    break;
                case "2":
                    result = "<span style='color:red'>PPAR submitted</span>";
                    break;
                case "3":
                    result = "<span style='color:red'>Ready for sampling</span>";
                    break;
                case "4":
                    result = "<span style='color:red'>not ready for sampling</span>";
                    break;
                case "5":
                    result = "<span style='color:red'>PPAR circulating</span>";
                    break;
                case "6":
                    if (FINALSTATUS == "0")
                        result = "<span style='color:red'>PPAR circulating</span>";
                    else
                        result = "<span style='color:blue'>PPAR released</span>";
                    break;
                case "7":
                    result = "<span style='color:blue'>PPAR decided</span>";
                    break;

                default:
                    result = "<span style='color:red'></span>";
                    break;
            }
            //}
            return result;
        }



        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            btnSubmit_Click(null, null);
        }

        /// <summary>
        /// Export
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string sql = " where 1=1 ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (txtSupplierNo.Text.Trim() != "")
                sql += " and suppliercode='" + txtSupplierNo.Text.Trim() + "' ";
            if (txtCreateStart.Text.Trim() != "")
                sql += " and trunc(endtime)>=to_date( '" + txtCreateStart.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtCreateEnd.Text.Trim() != "")
                sql += " and trunc(endtime)<=to_date( '" + txtCreateEnd.Text.Trim() + "', 'yyyy-mm-dd ') ";
            if (txtSamplingReportNo.Text.Trim() != "")
                sql += " and samplingreportno like '%" + txtSamplingReportNo.Text.Trim() + "%' ";
            if (txtPartNo.Text.Trim() != "")
                sql += " and bdntpartno='" + txtPartNo.Text.Trim() + "' ";
            if (ddlStatus.SelectedValue != "All")
            {
                if (ddlStatus.SelectedValue == "5")
                    sql += " and (status=5 or (status=6 and FINALSTATUS=0)) ";
                else
                    sql += " and status=" + ddlStatus.SelectedValue + " ";
            }
            if (ddlReason.SelectedValue != "All")
                sql += " and  reasonforsampling='" + ddlReason.SelectedValue + "' ";
            if (txtVersion.Text.Trim() != "")
                sql += " and PPARVERSION like '%" + txtVersion.Text.Trim() + "%' ";
            sql += " and status=7 ";
            DataSet ds = pbo.GetReportForSearch(sql);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["d1"] = r["ppardate"].ToString() == "" ? r["ppardate"] : DateTime.Parse(r["ppardate"].ToString()).ToString("yyyy-MM-dd");
            }
            ds.Tables[0].Columns.Remove("STATUS");
            ds.Tables[0].Columns.Remove("id");
            ds.Tables[0].Columns.Remove("PPARDATE");
            ds.Tables[0].Columns.Remove("FINALSTATUS");
            ds.Tables[0].Columns.Remove("REASONFORSAMPLING");

            ds.Tables[0].Columns["d1"].ColumnName = "Created date";
            ds.Tables[0].Columns["ROWNUM"].ColumnName = "No.";
            ds.Tables[0].Columns["SAMPLINGREPORTNO"].ColumnName = "Sampling report No.";
            ds.Tables[0].Columns["PPARNO"].ColumnName = "PPAR.No";
            ds.Tables[0].Columns["BDNTPARTNO"].ColumnName = "Part.No";
            ds.Tables[0].Columns["SUPPLIERCODE"].ColumnName = "Suppiler Code";
            ds.Tables[0].Columns["PPARVERSION"].ColumnName = "PPAR version";
            ds.Tables[0].Columns["ST"].ColumnName = "Status";


            CreateExcel(ds, "SampleCard");
        }
    }
}